package com.wyh.day05.exercises;

/**
 * @author 王艺豪
 * @version 1.0
 */
public class Exercise06 {
    public static void main(String[] args) {

        System.out.println(fib1(3)); //递归
        System.out.println(fib2(3)); //迭代
    }

    //斐波那契数列 Fibonacci
    //递归方法
    public static int fib1(int num) {

        if (num >= 1) {
            if (num == 1 || num == 2) return 1;
            return fib1(num - 1) + fib1(num - 2);
        } else System.out.println("输入的数字有误");
        return -1;
    }

    //循环
    public static int fib2(int num) {

        if (num >= 1) {
            if (num == 1 || num == 2) return 1;
            int up = 1;
            int upp = 1;
            int res = 0;
            for (int i = 3; i <= num; i++) {
                res = up + upp;
                upp = up;
                up = res;
            }
            return res;
        } else System.out.println("输入的数字有误");
        return -1;
    }

}
